package health

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/health"
	healthReq "gitcode.com/G-YT/ANN/server/model/health/request"
	"gitcode.com/G-YT/ANN/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PhysiqueApi struct {
}

var physiqueService = service.ServiceGroupApp.HealthServiceGroup.PhysiqueService

// CreatePhysique 創建Physique
// @Tags Physique
// @Summary 創建Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "創建Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /physique/createPhysique [post]
func (physiqueApi *PhysiqueApi) CreatePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.CreatePhysique(&physique); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeletePhysique 删除Physique
// @Tags Physique
// @Summary 删除Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "删除Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /physique/deletePhysique [delete]
func (physiqueApi *PhysiqueApi) DeletePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.DeletePhysique(physique); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeletePhysiqueByIds 批量删除Physique
// @Tags Physique
// @Summary 批量删除Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /physique/deletePhysiqueByIds [delete]
func (physiqueApi *PhysiqueApi) DeletePhysiqueByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.DeletePhysiqueByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdatePhysique 更新Physique
// @Tags Physique
// @Summary 更新Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "更新Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /physique/updatePhysique [put]
func (physiqueApi *PhysiqueApi) UpdatePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.UpdatePhysique(physique); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPhysique 用id査詢Physique
// @Tags Physique
// @Summary 用id査詢Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.Physique true "用id査詢Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /physique/findPhysique [get]
func (physiqueApi *PhysiqueApi) FindPhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindQuery(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rephysique, err := physiqueService.GetPhysique(physique.ID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"rephysique": rephysique}, c)
	}
}

// GetPhysiqueList 分页獲取Physique列表
// @Tags Physique
// @Summary 分页獲取Physique列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.PhysiqueSearch true "分页獲取Physique列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /physique/getPhysiqueList [get]
func (physiqueApi *PhysiqueApi) GetPhysiqueList(c *gin.Context) {
	var pageInfo healthReq.PhysiqueSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := physiqueService.GetPhysiqueInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

func (physiqueApi *PhysiqueApi) SetPhysiqueGraphic(c *gin.Context) {
	var req healthReq.PhysiqueGraphicReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.SetPhysiqueGraphic(req); err != nil {
		global.GVA_LOG.Error("设置失敗!", zap.Error(err))
		response.FailWithMessage("设置失敗", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

func (physiqueApi *PhysiqueApi) GetPhysiqueGraphic(c *gin.Context) {
	physique_id := c.Query("physique_id")
	if res, err := physiqueService.GetPhysiqueGraphic(physique_id); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithData(res, c)
	}
}
